const { crayfish4weapp } = require('../services/hosts')
const { User, ApiCreater } = require('../../dave/dave.min.js')

let cookie = ''
const getCookieString = response => {
  let header = response.data['set-cookie']
  if (Array.isArray(header)) {
    header = header.join(' ')
  }
  return header
}

const setCookie = response => {
  let body = JSON.parse(response.data.body)
  let newCookie = (getCookieString(response).match(/eleme__ele_me=([^;]+)/) || [])[1] || ''
  if (newCookie) {
    cookie = newCookie
  }
  response.data = body
  return response
}

const getCurrentShopId = () => {
  return wx.getStorageSync('CURRENT_RESTAURANT_ID')
}

let APIS = {

  /**
   * 微信登录
   * 如果已绑定微信的袋鼠公民账号，可以直接登录
   *
   * @param wechatCode
   * @returns {*}
   */
  loginByWechat: wechatCode => {
    return ApiCreater({
      url: `/pizza/v1/wechat/${wechatCode}`,
      method: 'POST'
    })
  },




  /**
   * 根据经纬度获取地址信息
   * https://github.com/eleme/biz_doc/blob/master/docs/mux/bgs/poi.md
   * @param options {object}
   * @param options.latitude 纬度
   * @param options.longitude 经度
   * @returns {*}
   */
  getGeohashPosition: ({ latitude, longitude }) => {
    return ApiCreater({
      url: `/web_api/v1/location/nearBy?latitude=${latitude}&longitude=${longitude}`
    })
  },

  /**
   * 获取一个位置的附近地址
   * @param geohash
   * @returns {*}
   */
  getNearbyAddress: (geohash) => {
    return ApiCreater({
      url: `/v2/pois?type=nearby&limit=3&geohash=${geohash}`
    })
  },
    register(data) {
        return ApiCreater({
            url: '/web_api/v1/session/register',
            method: 'POST',
            data,
        })
    },
    login(data) {
        return ApiCreater({
            url: '/web_api/v1/session/wetchatLogin',
            method: 'POST',
            data,
        })
    },

    /**
     * 发送短信
     * @param data
     */
    smsCode: (data) => ApiCreater({
        url: '/web_api/v1/session/smsCode',
        method: 'GET',
        data,
    }),
    /**
     * 注册时登入
     * @param data
     */
    login(data) {
        return ApiCreater({
            url: '/web_api/v1/session/wetchatLogin',
            method: 'POST',
            data,
        })
    },
    registerShopInfo(data) {
        return ApiCreater({
            url: '/web_api/v1/shopInfo/registerShopInfo',
            method: 'POST',
            data,
        })
    },

    /**
     * 查询用户店铺列表
     * @param data
     */
    listShop: (userId) => ApiCreater({
        url: '/web_api/v1/shopInfo/list/'+userId,
        method: 'POST'
    }),

    createRewardFunction(data) {
        return ApiCreater({
            url: '/web_api/v1/shopFunction/createRewardFunction',
            method: 'POST',
            data,
        })
    },

}

module.exports = APIS
